
from flask_restful import Resource

from restful.api__.session import  login_required_current_user_generate, \
    pid_required_target_post_generate
from restful.model.delete import DeleteInstance
from restful.model.query import QueryOneInstance
from restful.model.tables import Like
from restful.model.update import UpdateInstance
from restful.response import ResponseSuccess, ResponseConflict


class LikeOrCancel(Resource):
    @login_required_current_user_generate
    @pid_required_target_post_generate
    def post(self,target_post,current_user):
        like = QueryOneInstance(Like,{'pid':target_post.id,'uid':current_user.id})

        if like:
            return ResponseConflict("勿重复点赞")

        UpdateInstance(Like(uid=current_user.id,pid=target_post.id))
        return ResponseSuccess("已点赞")

    @login_required_current_user_generate
    @pid_required_target_post_generate
    def delete(self,target_post,current_user):
        like = QueryOneInstance(Like,{'uid':current_user.id,'pid':target_post.id})

        if not like:
            return ResponseConflict("点赞不存在")

        DeleteInstance(like)
        return ResponseSuccess("已取消点赞")
